package com.yoyo.ad.yoyoplat;

import android.content.Context;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;

import com.yoyo.ad.confusion.AdManage;
import com.yoyo.ad.main.IAdBannerListener;
import com.yoyo.ad.main.IAdClick;
import com.yoyo.ad.main.IAdFactory_;
import com.yoyo.ad.main.IAdInteractionListener;
import com.yoyo.ad.main.IAdView;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.utils.AnalysisUtils;
import com.yoyo.yoyoplat.sdk.YYNative;
import com.yoyo.yoyoplat.sdk.YYNativeResponse;

import java.util.ArrayList;
import java.util.List;

public class YPFactory implements IAdFactory_ {

    private IAdView mAdView;
    private IAdBannerListener mAdBannerListener;
    private IAdInteractionListener mAdInteractionListener;
    private Context mContext;

    private SparseArray<Long> adIds = new SparseArray<>();
    private SparseBooleanArray isIpReports = new SparseBooleanArray();
    private final int adType = AdManage.YOYO_PLAT;
    private YYNative mYyNative;

    public YPFactory(Context context) {
        mContext = context;
    }

    @Override
    public void setAdView(IAdView adView) {
        if (mAdView == null && adView != null)
            mAdView = adView;
    }


    @Override
    public void setAdBannerListener(IAdBannerListener adView) {
        if (mAdBannerListener == null && adView != null)
            mAdBannerListener = adView;
    }

    @Override
    public void setAdInteractionListener(IAdInteractionListener adInteractionListener) {
        if (mAdInteractionListener == null && adInteractionListener != null)
            mAdInteractionListener = adInteractionListener;
    }

    @Override
    public void setConfig(long adId, int adPositionId, boolean isIpReport) {
        adIds.put(adPositionId, adId);
        isIpReports.put(adPositionId, isIpReport);
    }


    @Override
    public void getYoyoAd(final int position, final int requestCode, int size, String adPlaceId, int type) {
        mYyNative = new YYNative(mContext, Integer.valueOf(adPlaceId), 10001);
        mYyNative.setYoYoNativeListener(new YYNative.YoYoNativeListener() {
            @Override
            public void success(List<YYNativeResponse> arg0) {
                AdManage.showAdSuccess(mContext, AdManage.TENCENT, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
                int size1 = arg0.size();
                List<YoYoAd> list = new ArrayList<>(size1);
                for (YYNativeResponse n : arg0) {
                    list.add(new YPAd(n, new IAdClick() {
                        @Override
                        public void onAdClick(View view) {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_CLICK);
                            if (mAdView != null)
                                mAdView.onAdClick(requestCode, view);
                        }

                        @Override
                        public void onAdShow() {
                            AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_PRESENT);
                        }
                    }));
                }
                if (mAdView != null)
                    mAdView.adSuccess(requestCode, list);
            }

            @Override
            public void fail(String code, String err) {
                AnalysisUtils.addStatistics(adIds.get(position, 0L), AnalysisUtils.TYPE_FAIL);
                mAdView.adFail(requestCode, "错误码:" + code + "  错误原因:" + err);
                AdManage.showAdSuccess(mContext, AdManage.TENCENT, adIds.get(position, 0L),
                        position, isIpReports.get(position, false));
            }
        });
        mYyNative.loadData();
    }

    @Override
    public void getExpressAd(int position, int requestCode, String adPlaceId, int size, int width) {

    }

    @Override
    public void getBanner(int position, int requestCode, String adPlaceId, ViewGroup adsParent, int width, int height) {

    }

    @Override
    public void getInteraction(int position, int requestCode, String adPlaceId) {

    }

    @Override
    public void onDestroy() {
        if (mYyNative != null)
            mYyNative.destroy();
    }
}
